package com.yuntian.service.impl;

import com.yuntian.mapper.DeptMapper;
import com.yuntian.mapper.EmpMapper;
import com.yuntian.pojo.Dept;
import com.yuntian.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<Dept> list() {

        return deptMapper.list();
    }

    @Transactional(rollbackFor = Exception.class)//作用：将当前方法交给spring进行事务管理，方法执行前，开启事务；成功执行完毕，提交事务；出现异常，回滚事务
    @Override
    public void delete(Integer id) {

        deptMapper.deleteById(id);//根据id删除部门信息
        empMapper.deletByDepyid(id);//根据部门id删除该部门下的员工
    }

    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept);
    }
    @Override
    public List<Dept> SelectById(Integer id) {

        return deptMapper.SelectById(id);
    }

    @Override
    public void updateData(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.updateData(dept);
    }

}
